const Tools = {
  // 获取压平之后的范围数据
  GetFlattenRegion(arr) {
    let res = [];
    for (let i = 0; i < arr.length; i++) {
      const cu = arr[i]
      res.push(cu[0], cu[1], cu[2]);
    };
    return res;
  },
  // 接受 config 内的配置，将树结构含有服务的节点检出
  GetServiceData(arr) {
    if (!arr || !arr.length) { return [] }
    let res = [];
    for (let i = 0; i < arr.length; i++) {
      const tem = arr[i];
      // 含有服务的图层须有 url 字段
      if (tem.url) {
        res.push(tem)
      } else {
        if (tem.children) {
          res = res.concat(Tools.GetServiceData(tem.children))
        }
      }
    }
    return res
  },
  GetFunctionData(arr) {
    if (!arr || !arr.length) { return [] }
    let res = [];
    for (let i = 0; i < arr.length; i++) {
      const tem = arr[i];
      // type 为 function
      if (tem.type === 'function') {
        res.push(tem)
      } else {
        if (tem.children) {
          res = res.concat(Tools.GetFunctionData(tem.children))
        }
      }
    }
    return res
  },
  // start 代表起始点
  // end 代表结束点
  // segment 代表建该段距离分为几个段，即补 segment - 1 个点
  GetNextPositions(start, end, segment) {
    if (!segment) { return [] }
    const res = [];
    for (let i = 1; i < segment; i++) {
      const cartesian = Cesium.Cartesian3.lerp(
        Cesium.Cartesian3.fromDegrees(start[0], start[1], start[2]),
        Cesium.Cartesian3.fromDegrees(end[0], end[1], end[2]),
        i / segment,
        new Cesium.Cartesian3()
      )

      const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
      res.push([
        Cesium.Math.toDegrees(cartographic.longitude),
        Cesium.Math.toDegrees(cartographic.latitude),
        cartographic.height
      ])
    }
    return res;
  },
  // 日期格式化
  GetFormatDate(date) {
    const d = new Date(date)
    const y = d.getFullYear() // 年份
    const m = (d.getMonth() + 1).toString().padStart(2, '0') // 月份
    const r = d.getDate().toString().padStart(2, '0') // 日子
    const hh = d.getHours().toString().padStart(2, '0') // 小时
    const mm = d.getMinutes().toString().padStart(2, '0') // 分钟
    const ss = d.getSeconds().toString().padStart(2, '0') // 秒
    return `${y}-${m}-${r} ${hh}:${mm}:${ss}`// es6 字符串模板
  },
  // 重置S3M图层的透明度
  ResetOpacityOfS3MLayer(layerS3M, v) {
    layerS3M.style3D.fillForeColor.alpha = v;
    layerS3M.style3D.lineColor.alpha = v;
    layerS3M.style3D.pointColor.alpha = v;
  },
  // 重置影像图层的透明度
  ResetOpacityOfImageLayer(layerIm, v) {
    layerIm && (layerIm.alpha = v);
  },
  // 定位到某个点
  /**
   * @description: 飞行定位到一个笛卡尔空间直角坐标点位置
   * @param {Cartesian3} destination 目标点 Cartesian3
   * @param {Number} heading  默认=0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角 垂直向下， ENU局部坐标系中XY平面的旋转角度，平面下为负，上为正，
   * @param {*} range    =0.0   距目标点距离
   * @param {*} duration =3   持续时间
   * @param {*} callBack =null   回调函数，定位完成后执行
   */
  FlyToPoint(destination, heading = 0.0, pitch = -90, range = 0.0, duration = 3, callBack = null) {
    if (!window.viewer) {
      console.log('三维球未初始化！');
      return;
    }
    if (!destination) {
      console.log('定位目标点不对！');
      return;
    }
    const boundingSphere = new Cesium.BoundingSphere(destination, 0.0);
    window.viewer.camera.flyToBoundingSphere(boundingSphere, {
      duration: duration,
      maximumHeight: 100,
      complete: function () {
        if (callBack) {
          callBack();
        } else {
          console.log('定位失败！');
        }
      },
      cancel: function () {
        console.log('定位取消！');
      },
      offset: {
        heading: Cesium.Math.toRadians(heading),
        pitch: Cesium.Math.toRadians(pitch),
        range: range
      },
    });
  },
  // 根据起始点终止点确定航向 -> 起点指向终点, 0 时指向北方
  GetHeadingOfBoat({ start, end }) {
    if (!turf) { return 0 };
    const startPoint = turf.point([
      start.longitude,
      start.latitude,
    ]);
    const endPoint = turf.point([end.longitude, end.latitude]);
    // 计算之后的角度
    const cumputingAng = turf.bearing(startPoint, endPoint);
    return (cumputingAng * Math.PI) / 180;
  },
  RgbToHex(r, g, b) {
    var hex = ((r << 16) | (g << 8) | b).toString(16);
    return "#" + new Array(Math.abs(hex.length - 7)).join("0") + hex;
  },
  HexToRgb(hex) {
    var rgb = [];
    for (var i = 1; i < 7; i += 2) {
      rgb.push(parseInt("0x" + hex.slice(i, i + 2)));
    }
    return rgb;
  },
  GetGradientColor(startColor, endColor, step) {
    // 将 hex 转换为rgb
    var sColor = Tools.HexToRgb(startColor),
      eColor = Tools.HexToRgb(endColor);

    // 计算R\G\B每一步的差值
    var rStep = (eColor[0] - sColor[0]) / step;
    gStep = (eColor[1] - sColor[1]) / step;
    bStep = (eColor[2] - sColor[2]) / step;

    var gradientColorArr = [];
    for (var i = 0; i < step; i++) {
      // 计算每一步的hex值
      gradientColorArr.push(Tools.RgbToHex(parseInt(rStep * i + sColor[0]), parseInt(gStep * i + sColor[1]), parseInt(bStep * i + sColor[2])));
    }
    return gradientColorArr;
  }

}
export default Tools;